#include "function.h"
#include "tim.h"
#include "task.h"

#include "stdio.h"
#include "usart.h"
#include "key.h"
#include "led.h"
#include "utils.h"
#include "rf.h"


void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
    /* 如果是tim14中断 - 执行任务调度切换 */
    if(htim == &htim14){
        TaskScheduleLoop();
    }
}


void task_controller_error(void){
    while(1);
}


TaskStateShift LED_SystemRunningFlashTask(TaskCallState state){
    led1.Shift();
    return TASK_TO_WATE;
}




int fputc(int ch, FILE *f)
{
    uint8_t temp[1] = {ch};
    HAL_UART_Transmit(&huart1, temp, 1, 2);
    return ch;
}


void Key1_Callback(KeyEvent e){
    
    if(Key_CheckEvent(e,KEY_EVENT_PRESSED)){
        //printf("key1 Press!\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_RELEASED)){
        //printf("key1 Up!\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_LONG_PRESSED)){
        //printf("key1 Long Press!\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_LONG_PRESSED_REPEAT)){
        printf("key1 Long Press Still!\r\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_LONG_PRESSED_OVER)){
        //printf("key1 Long Press Over!\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_MULTI_CLICKED)){
        if(key1->getClickCount(key1)==2){
            RF_SetTxAddr((uint8_t[])RF_RADIO_CHANNEL, RF_ADDR_WIDTH);
            // RF_TxPacket((uint8_t*)"YYY\r\n", 6);
            RF_SetMode(RF_MODE_TX);
            RF_TxDataDirect((uint8_t*)"YYY\r\n", 6);
            RF_SetMode(RF_MODE_RX);
            //printf("key1 Double Click!\n");
        }else{
            //printf("key1 %d Click!\n",key1->getClickCount(key1));
            
        }
        
    }
}


void Key2_Callback(KeyEvent e){
    
    if(Key_CheckEvent(e,KEY_EVENT_PRESSED)){
        //printf("key2 Press!\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_RELEASED)){
        //printf("key2 Up!\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_LONG_PRESSED)){
        //printf("key2 Long Press!\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_LONG_PRESSED_REPEAT)){
        printf("key2 Long Press Still!\r\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_LONG_PRESSED_OVER)){
        //printf("key2 Long Press Over!\n");
    }

    if(Key_CheckEvent(e,KEY_EVENT_MULTI_CLICKED)){
        if(key2->getClickCount(key2)==2){
            RF_SetTxAddr((uint8_t[])RF_RADIO_CHANNEL, RF_ADDR_WIDTH);
            // RF_TxPacket((uint8_t*)"NN\r\n", 5);
            RF_SetMode(RF_MODE_TX);
            RF_TxDataDirect((uint8_t*)"NN\r\n", 5);
            RF_SetMode(RF_MODE_RX);
            //printf("key2 Double Click!\n");
        }else{
            //printf("key2 %d Click!\n",key2->getClickCount(key2));

            if(key2->getClickCount(key2)==3){
                if(RF_Check()){
                    uint8_t addr[5];
                    Get5BytesMacAddress(addr);
                    printf("RF found! ADDR:0x%02X%02X%02X%02X%02X\r\n", addr[4], addr[3], addr[2], addr[1], addr[0]);

                }else{
                    printf("RF not found!\r\n");
                }
            }

        }
        
    }
}


